
package com.epam.android.training.day7;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * @author pavel_busko
 * 
 * Sample Activity to test service and receiver
 */
public class Day7Activity extends Activity {

    private Button buttonStartService;

    private OnClickListener serviceStarter;

    private OnClickListener serviceStoper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Intent serviceIntent = new Intent(getApplicationContext(), TestService.class);

        buttonStartService = (Button) findViewById(R.id.button_start_service);
        serviceStarter = new OnClickListener() {
            @Override
            public void onClick(View v) {
                startService(serviceIntent);
                switchButtonToStop();
            }
        };
        serviceStoper = new OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(serviceIntent);
                switchButtonToStart();
            }
        };
        buttonStartService.setOnClickListener(serviceStarter);
    }

    private void switchButtonToStart() {
        buttonStartService.setOnClickListener(serviceStarter);
        buttonStartService.setText(R.string.button_start_service_label + "-"
                + (Looper.getMainLooper().getThread() == Thread.currentThread()));
    }

    private void switchButtonToStop() {
        buttonStartService.setOnClickListener(serviceStoper);
        buttonStartService.setText(R.string.button_stop_service_label);
    }

    @Override
    protected void onDestroy() {
        Log.i("TestActivity", "destr");
        super.onDestroy();
    }

}
